<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" class="queryForms">
			<el-form-item label="产品名称" prop="productName">
				<el-input maxlength="20" show-word-limit v-model="queryParams.productName" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="产品型号" prop="productModel">
				<el-input maxlength="20" show-word-limit v-model="queryParams.productModel" placeholder="请输入产品型号" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="状态" prop="state">
				<el-select v-model="queryParams.state" placeholder="状态" clearable>
					<el-option v-for="dict in dict.type.default_status" :key="dict.value" :label="dict.label" :value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['erp:product:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['erp:product:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="info" icon="el-icon-upload2" size="mini" @click="handleImport('product')" v-hasPermi="['erp:product:import']">导入产品</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="info" icon="el-icon-upload2" size="mini" :disabled="multiple" @click="handleImport('part')" v-hasPermi="['erp:product:importPart']">导入零组件</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="info" icon="el-icon-upload2" size="mini" :disabled="multiple" @click="handleImport('mustPart')" v-hasPermi="['erp:product:importMustPart']">导入必换件</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="info" icon="el-icon-upload2" size="mini" :disabled="multiple" @click="handleImport('Redundancy')" v-hasPermi="['erp:product:importRedundancy']">导入多余物</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="info" size="mini" :disabled="multiple" @click="handleManys" v-hasPermi="['erp:product:step']">产品步骤时间</el-button>
			</el-col>
			<right-toolbar @changeTableData="changeTableData" menuNames="productStep27" :showSearch.sync="showSearch" @queryTable="getList" :columns.sync="columns" perm="erp:product:list"></right-toolbar>
		</el-row>

		<div :class="sidebar.opened && eleTabTop <= 85 ? 'eleTab' : !sidebar.opened && eleTabTop <= 85 ? 'eleTabs' : ''" class="informationTab" ref="eleTab">
			<el-table ref="table" @row-click="row_click" v-loading="loading" :data="productList" @selection-change="handleSelectionChange" border height="100%">
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="序号" align="center" type="index" width="55" />
				<el-table-column label="产品名称" prop="productName" min-width="150" :show-overflow-tooltip="true" v-if="columns[0].visible" />
				<el-table-column label="产品型号" prop="productModel" min-width="150" :show-overflow-tooltip="true" v-if="columns[1].visible" />
				<el-table-column label="状态" align="center" prop="state" min-width="150" :show-overflow-tooltip="true" v-if="columns[2].visible">
					<template slot-scope="scope">
						<el-switch v-model="scope.row.state" active-value="1" inactive-value="0" @change="handleStatusChange(scope.row)"> </el-switch>
					</template>
				</el-table-column>
				<el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
					<template slot-scope="scope">
						<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['erp:product:edit']">修改</el-button>
						<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['erp:product:remove']">删除</el-button>
						<el-dropdown size="mini" @command="command => handleCommand(command, scope.row)" v-hasPermi="['erp:product:productPart', 'erp:product:productMustPart']">
							<el-button size="mini" type="text" icon="el-icon-d-arrow-right">更多</el-button>
							<el-dropdown-menu slot="dropdown">
								<el-dropdown-item command="handleComponent" v-hasPermi="['erp:product:productPart']">零组件</el-dropdown-item>
								<el-dropdown-item command="handleQuick" v-hasPermi="['erp:product:productMustPart']">必换件</el-dropdown-item>
								<el-dropdown-item command="goPage" v-hasPermi="['erp:product:step']">产品步骤时间</el-dropdown-item>
								<el-dropdown-item command="gocontroller" v-hasPermi="['erp:product:controller']">多余控制物</el-dropdown-item>
							</el-dropdown-menu>
						</el-dropdown>
					</template>
				</el-table-column>
			</el-table>
		</div>

		<pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

		<!-- 添加或修改产品列对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="900px" append-to-body v-dialogDrag>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px" class="add-form2">
				<el-form-item label="产品名称" prop="productTypeId">
					<el-select v-model="form.productTypeId" placeholder="产品名称" @change="changeproduct">
						<el-option v-for="dict in dict.type.default_product" :key="dict.value" :label="dict.label" :value="dict.value" />
					</el-select>
				</el-form-item>
				<el-form-item label="产品型号" prop="productModel">
					<el-input v-model="form.productModel" placeholder="请输入产品型号" maxlength="20" show-word-limit />
				</el-form-item>
				<el-form-item label="客户状态" prop="state">
					<el-radio-group v-model="form.state">
						<el-radio v-for="dict in dict.type.default_status" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
		<el-dialog :title="ImportTitle" :visible.sync="ImportOpen" width="400px" append-to-body v-dialogDrag>
			<ImportTemplete :downLoadTemplete="downLoadTemplete" :importUrl="importUrl" @closeOpen="closeOpen" :selectData="ids"></ImportTemplete>
		</el-dialog>

		<el-dialog title="多余物控制检查模版" :visible.sync="openTemplate" width="900px" append-to-body v-dialogDrag>
			<!-- <div style="height: 70vh; overflow-y: auto; padding-right: 15px"> -->
			<el-table :data="detailscomponentsList" style="margin-bottom: 20px" @row-click="handleRowClick" border>
				<el-table-column label="序号" align="center" type="index" width="55" />
				<el-table-column label="工序号" prop="processNumber" min-width="170" align="center">
					<template slot-scope="scope">
						<el-input-number v-model="scope.row.processNumber" :min="1" :max="10000000"></el-input-number>
					</template>
				</el-table-column>
				<el-table-column label="工序名称" prop="processName" min-width="150" show-overflow-tooltip="" align="center">
					<template slot-scope="scope">
						<el-input v-model="scope.row.processName" maxlength="200"></el-input>
					</template>
				</el-table-column>
				<el-table-column label="操作" class-name="small-padding fixed-width" width="130px" fixed="right" align="center">
					<template slot-scope="scope">
						<el-button size="mini" type="text" icon="el-icon-plus" @click="addComponents(scope.row)">添加</el-button>
						<el-button size="mini" type="text" icon="el-icon-delete" @click="deleteComponents(scope.$index, scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination v-show="totalcontrol > 0" :total="totalcontrol" :page.sync="queryParamsControl.pageNum" :limit.sync="queryParamsControl.pageSize" @pagination="getcontrolData" />
			<!-- </div> -->
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitFormcomponents">确 定</el-button>
				<el-button @click="openTemplate = false">取 消</el-button>
			</div>
		</el-dialog>

		<el-dialog title="修改产品步骤时间" :visible.sync="openManys" width="900px" append-to-body v-dialogDrag>
			<div style="height: 70vh; overflow-y: auto">
				<el-table :data="tableDatas" border>
					<el-table-column label="序号" align="center" width="55" type="index" />
					<el-table-column label="工艺流名称" prop="technologyName" :show-overflow-tooltip="true" align="center" />
					<el-table-column label="工艺流编码" prop="technologyCode" :show-overflow-tooltip="true" align="center" />
					<el-table-column label="工艺流字典" prop="technologyDictCode" :show-overflow-tooltip="true" align="center" />
					<el-table-column label="计划完成时间" prop="plannedTime" :show-overflow-tooltip="true" align="center">
						<template slot-scope="scope">
							<el-input v-model.number="scope.row.plannedTime" oninput="value=value.replace(/[^\d]/g,'')" placeholder="请输入计划完成时间" maxlength="5" />
						</template>
					</el-table-column>
				</el-table>
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitFormManys">确 定</el-button>
				<el-button @click="openManys = false">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { getListData1 } from "@/api/product/productStep"
import { batchEditStep, listProduct, getProduct, delProduct, addProduct, updateProduct, batchAdd, getRedundancy } from "@/api/product/product"
import ImportTemplete from "@/views/components/ImportTemplete"
import { mapGetters } from "vuex"
import { getcolumn } from "@/api/selectList"
import DocumentShow from "@/views/components/DocumentShow"
export default {
	name: "Product_menu",
	components: { ImportTemplete, DocumentShow },
	dicts: ["default_status", "default_product"],
	computed: {
		...mapGetters(["sidebar"])
	},
	data() {
		return {
			totalcontrol: 0,
			tableDatas: [],
			openManys: false,
			openTemplate: false,
			editRowComponent: {},
			detailscomponentsList: [],
			fileVedioList: [],
			showfileListDialog: false,
			showTodoListDialog: false,
			eleTabTop: 0, //表格容器距浏览器顶部距离
			// 导入产品接口
			importUrl: "/erp/product/importData",
			downLoadTemplete: {
				url: "/erp/product/importTemplate",
				title: "产品_模板"
			},
			ImportTitle: "导入产品",
			// 导入产品
			ImportOpen: false,
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 产品列表格数据
			productList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			queryParamsControl: {
				pageNum: 1,
				pageSize: 10,
				productId: null
			},
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				productTypeId: null,
				state: null,
				productName: null,
				productModel: null
			},
			// 表单参数
			form: { state: "1" },
			// 表单校验
			rules: {
				productTypeId: [{ required: true, message: "产品名称不能为空", trigger: "blur" }],
				productModel: [{ required: true, message: "产品型号不能为空", trigger: "blur" }]
			},
			controlId: "",
			// 列信息
			columns: [
				{ key: 0, label: `产品名称`, visible: true },
				{ key: 1, label: `产品型号`, visible: true },
				{ key: 2, label: `状态`, visible: true }
			]
		}
	},
	created() {
		getcolumn({ pageName: "Pagination31" }).then(response => {
			this.queryParams.pageSize = response.data.length ? (response.data[0].limit ? response.data[0].limit : 10) : 10
			this.getList()
		})
		// this.getList()
	},
	mounted() {
		// 滚动条的获取
		this.handleScrollx()
		window.addEventListener("scroll", this.handleScrollx, true)
	},
	activated() {
		this.getList()
		//获取表格容器距页面头部的距离
		this.handleScrollx()
	},
	destroyed() {
		window.removeEventListener("scroll", this.handleScrollx, true)
	},
	methods: {
		submitFormManys() {
			batchEditStep({
				ids: this.ids,
				tproductSteps: this.tableDatas
			}).then(res => {
				this.$modal.msgSuccess("修改成功")
				this.openManys = false
			})
		},
		handleManys() {
			getListData1({
				productId: this.ids[0]
			}).then(res => {
				this.openManys = true
				this.tableDatas = res.rows
			})
		},
		// 单击表格行
		row_click(row, column, event) {
			if (column && column.label == "操作") {
				return
			} else {
				this.$refs.table.toggleRowSelection(row)
			}
		},
		changeTableData() {
			this.$nextTick(() => {
				this.$refs.table.doLayout()
			})
		},

		submitFormcomponents() {
			this.editRowComponent.redundancies = this.detailscomponentsList
			let flag = this.detailscomponentsList.every(item => {
				return item.processNumber && item.processName
			})
			if (!flag) {
				this.$modal.msgError("请将数据填写完整")
				return false
			}
			batchAdd(this.editRowComponent).then(res => {
				this.$modal.msgSuccess("设置成功")
				this.openTemplate = false
			})
		},
		addComponents(row, index) {
			let data = this.detailscomponentsList
			if (data[data.length - 1].processNumber && data[data.length - 1].processName) {
				data.push({
					processNumber: undefined,
					processName: undefined
				})
			} else {
				this.$modal.msgError("请将数据填写完整")
			}
		},
		deleteComponents(index, row) {
			this.detailscomponentsList.splice(index, 1)
		},
		handleRowClick(row, column, event) {
			console.log("行点击事件触发", row, column, event)
		},
		//获取表格容器距页面头部的距离
		handleScrollx() {
			this.eleTabTop = this.$refs.eleTab.getBoundingClientRect().top
		},
		closeOpen(e) {
			this.ImportOpen = false
			if (e) {
				this.getList()
			}
		},
		handleImport(type) {
			if (type == "product") {
				this.importUrl = "/erp/product/importData"
				this.downLoadTemplete = {
					url: "/erp/product/importTemplate",
					title: "产品_模板"
				}
				this.ImportTitle = "导入产品"
				this.ImportOpen = true
			} else if (type == "part") {
				this.importUrl = "/erp/productPart/importData"
				this.downLoadTemplete = {
					url: "/erp/productPart/importTemplate",
					title: "产品零组件_模板"
				}
				this.ImportTitle = "导入产品零组件"
				this.ImportOpen = true
			} else if (type == "Redundancy") {
				this.importUrl = "/erp/part/importData1"
				this.downLoadTemplete = {
					url: "/erp/part/importRedundancy",
					title: "多余物_模板"
				}
				this.ImportTitle = "导入多余物"
				this.ImportOpen = true
			} else {
				this.importUrl = "/erp/productMustPart/importData"
				this.downLoadTemplete = {
					url: "/erp/productMustPart/importTemplate",
					title: "产品零组件必换件_模板"
				}
				this.ImportTitle = "导入产品零组件必换件"
				this.ImportOpen = true
			}
		},
		// 更多操作触发
		handleCommand(command, row) {
			switch (command) {
				// 零组件
				case "handleComponent":
					this.handleComponent(row)
					break
				// 必换件
				case "handleQuick":
					this.handleQuick(row)
					break
				case "gocontroller":
					this.gocontroller(row)
					break
				case "goPage":
					this.$router.push({ path: "/product/productStep", query: { id: row.id } })
				default:
					break
			}
		},
		handleComponent(row) {
			this.$router.push("/product/productSetting/productPart/index/" + row.id)
		},
		handleQuick(row) {
			this.$router.push("/product/productSetting/productMustPart/index/" + row.id)
		},
		gocontroller(row) {
			this.controlId = row.id
			this.editRowComponent = row
			this.queryParamsControl.productId = this.controlId
			this.getcontrolData()
			this.openTemplate = true
		},
		getcontrolData() {
			getRedundancy(this.queryParamsControl).then(res => {
				this.detailscomponentsList = res.rows.length > 0 ? res.rows : [{}]
				this.totalcontrol = res.total
			})
		},
		// 状态修改
		handleStatusChange(row) {
			this.$modal
				.confirm("确认要修改产品状态吗？")
				.then(function () {
					return updateProduct({
						id: row.id,
						state: row.state
					})
				})
				.then(() => {
					this.$modal.msgSuccess("修改成功")
				})
				.catch(function () {
					row.state = row.state === "0" ? "1" : "0"
				})
		},
		// 选择产品
		changeproduct(e) {
			this.dict.type.default_product.forEach(element => {
				if (e == element.value) {
					this.form.productName = element.label
				}
			})
		},
		/** 查询产品列列表 */
		getList() {
			this.loading = true
			listProduct(this.queryParams).then(response => {
				this.productList = response.rows
				this.total = response.total
				this.loading = false
			})
		},
		// 取消按钮
		cancel() {
			this.open = false
			this.reset()
		},
		// 表单重置
		reset() {
			this.form = {
				id: null,
				productTypeId: null,
				productName: null,
				productModel: null,
				remark: null,
				state: "1"
			}
			this.resetForm("form")
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1
			this.getList()
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm")
			this.handleQuery()
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.id)
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset()
			this.open = true
			this.title = "添加产品"
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset()
			const id = row.id || this.ids
			getProduct(id).then(response => {
				this.form = response.data
				this.open = true
				this.title = "修改产品"
			})
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					if (this.form.id != null) {
						updateProduct(this.form).then(response => {
							this.$modal.msgSuccess("修改成功")
							this.open = false
							this.getList()
						})
					} else {
						addProduct(this.form).then(response => {
							this.$modal.msgSuccess("新增成功")
							this.open = false
							this.getList()
						})
					}
				}
			})
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const ids = row.id || this.ids
			this.$modal
				.confirm("是否确认删除产品数据项？")
				.then(function () {
					return delProduct(ids)
				})
				.then(() => {
					this.getList()
					this.$modal.msgSuccess("删除成功")
				})
				.catch(() => {})
		},
		/** 导出按钮操作 */
		handleExport() {
			this.download(
				"erp/product/export",
				{
					...this.queryParams
				},
				`product_${new Date().getTime()}.xlsx`
			)
		}
	}
}
</script>
